package com.kvest.mobile_newspaper.android;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.kvest.mobile_newspaper.R;
import com.kvest.mobile_newspaper.datamodel.DateElement;
import com.kvest.mobile_newspaper.datamodel.Element;
import com.kvest.mobile_newspaper.datamodel.ElementsContainer;

public class ClubListAdapter extends ArrayAdapter<Element>
{
    private static final int TYPE_ELEMENT = 0;
    private static final int TYPE_DATE = 1;
	private LayoutInflater inflater;
	
	private Drawable deleteImage;
	private Drawable actionsImage;
	
	private ActionsListener actionsListener;
	private OnClickListener onClickListener;
	
	public ClubListAdapter(Context context, int textViewResourceId, ArrayList<Element> items) 
    {
        super(context, textViewResourceId, items);
        this.inflater = LayoutInflater.from(context);
        
        deleteImage = getContext().getResources().getDrawable(R.drawable.delete);
        actionsImage = getContext().getResources().getDrawable(R.drawable.actions);
        
        onClickListener = new OnClickListener() 
    	{	
			public void onClick(View v) 
			{
				if (actionsListener != null)
				{
					actionsListener.elementAction((Element)v.getTag());
				}					
			}
		};
        actionsListener = null;
    }

	public void setaAtionsListener(ActionsListener actionsListener)
	{
		this.actionsListener = actionsListener;
	}

    public View getDateView(int position, View view, ViewGroup parent) {
        if (view == null)
        {
            view = this.inflater.inflate(R.layout.date_list_item, null);
        }

        Element element = getItem(position);
        TextView name = (TextView) view.findViewById(R.id.club_list_item_text);

        if (element != null && name != null)
        {
            name.setText(element.getName());
        }

        return view;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent)
    {
        if (getItem(position) instanceof DateElement) {
            return getDateView(position, view, parent);
        }

        if (view == null) 
        {
        	view = this.inflater.inflate(R.layout.club_list_item, null);
        }
        Element element = getItem(position);
        TextView name = (TextView) view.findViewById(R.id.club_list_item_text);
          
        if (element != null && name != null) 
        {
        	name.setText(element.getName());
        }

        ImageView iv = (ImageView)view.findViewById(R.id.iv_action);
        if (iv != null)
        {
        	iv.setImageDrawable((element instanceof ElementsContainer ? actionsImage : deleteImage));
        	iv.setTag(element);
        	iv.setOnClickListener(onClickListener);
        }
        
        return view;
    }
	
	public interface ActionsListener
	{
		public void elementAction(Element element);
	}

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        if (getItem(position) instanceof DateElement) {
            return TYPE_DATE;
        } else {
            return TYPE_ELEMENT;
        }
    }
}
